import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import './plugins/element.js'
import axios from 'axios'
import VueAxios from 'vue-axios'
import "../public/globalStatic.css"

Vue.prototype.setLocalValue =function(name,value){
    window.localStorage.setItem(name,JSON.stringify(value))
}

Vue.prototype.getLocalValue =(name)=>{
  JSON.parse(window.localStorage.getItem(name))

}
Vue.prototype.clearLocalValue =(name)=>{
  window.localStorage.clear()

}



// 添加请求拦截器
axios.interceptors.request.use(function (config) {
  // 在发送请求之前做些什么
  config.headers.Authorization=JSON.parse(window.localStorage.getItem('token'))
  return config;
}, function (error) {
  // 对请求错误做些什么
  return Promise.reject(error);
});



// 设置axios拦截器: 响应拦截器
axios_instance.interceptors.response.use(res => {
  // 成功响应的拦截
  return Promise.resolve(res.data)
},

// 添加响应拦截器
axios.interceptors.response.use(function (response) {
  // 对响应数据做点什么
      
  return response;
},
 function (err) {
    if(err.response){
      switch(err.response.status){
        // 对得到的状态码的处理，具体的设置视自己的情况而定
        case 401:
            console.log('未登录')
            window.location.href='/'
            break
        case 404:
            window.location.href='/'
            break
        case 405:
            console.log('不支持的方法')
            break
        case 200:
            console.log("请求成功");
            break
        default:
            console.log('其他错误')
            break
      }}}}}
  return Promise.reject(err);
});
axios.defaults.baseURL = 'http://127.0.0.1:8888/api/private/v1/';
  Vue.prototype.$http= axios

Vue.use(VueAxios, axios)
Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')

